<?php

use yii\helpers\Url;

$this->context->layout = false;
//使用率
echo $this->render("@app/views/public/useRate.php")
?>
<head>
    <link rel="stylesheet" href="/web/css/kendo/kendo.mobile.all.min.css" type="text/css"/>
    <link rel="stylesheet" href="/web/css/kendo/kendo.blueopal.mobile.min.css" type="text/css"/>
    <style>
        .km-switch-on, .km-switch-off {
            border-radius: 5px;
        }
    </style>
</head>
<div id="Home-Type-Edit-setting-List"></div>
<script>
    $(document).ready(function () {
        var Home_Type_Edit_setting_List = $("#Home-Type-Edit-setting-List");
        Home_Type_Edit_setting_List.kendoTreeList({
            dataSource: {
                transport: {
                    read: function (e) {
                        $.ajax({
                            cache: false,
                            type: "GET",
                            dataType: "json",
                            url: "<?=Url::toRoute(['/hr/pick/default/departmenttypeeditlist']); ?>",
                            success: function (data) {
                                e.success(data);
                            }
                        });
                    },
                    create: function (e) {
                        $.ajax({
                            cache: false,
                            type: "POST",
                            dataType: "json",
                            url: "<?=Url::toRoute(['/hr/pick/default/adddepartmenttypeedit']); ?>",
                            data: e.data,
                            success: function (result) {
                                if (result.status == 1) {
                                    var h_grid = Home_Type_Edit_setting_List.data("kendoTreeList");
                                    h_grid.dataSource.read();
                                    h_grid.refresh();
                                } else {
                                    layer.msg(result.msg, {icon: 5});
                                }
                            }
                        });
                    },
                    update: function (e) {
                        $.ajax({
                            cache: false,
                            type: "POST",
                            dataType: "json",
                            url: "<?=Url::toRoute(['/hr/pick/default/updatedepartmenttypeedit']); ?>",
                            data: e.data,
                            success: function (result) {
                                if (result.status == 1) {
                                    if (e.data.status==false&&e.data.hasChildren==true){
                                        var _treeList = Home_Type_Edit_setting_List.data("kendoTreeList");
                                        _treeList.dataSource.read();
                                        Home_Type_Edit_setting_List.data("kendoTreeList").refresh();
                                    }else {
                                        e.success();
                                    }
                                } else {
                                    layer.msg(result.msg, {icon: 5});
                                }
                            }
                        });
                    }
                },
                schema: {
                    model: {
                        id: "id",
                        parentId: "pid",
                        fields: {
                            id: {type: "string", nullable: false},
                            pid: {field: "pid", type: "string", nullable: true},
                            name: {field: "name", type: "string", validation: {required: true}},
                            sort: {field: "sort", type: "number", validation: {min: 0, required: true}},
                            status: {field: "status", type: "boolean", defaultValue: true}
                        }
                        //expanded: false
                    }
                },
                filter: [{field: "status", operator: "eq", value: true}]
            },
            dataBound: function () {
                var treeList = this;
                var trs = this.tbody.find('tr').each(function () {
                    var item = treeList.dataItem($(this));
                    if (item.pid == null) {
                        $(this).find('.k-grid-edit,.k-grid-delete').hide();
                    }else{
                        $(this).find('.k-grid-edit,.k-grid-delete').show();
                    }
                });
            },
            messages: {
                loading: "加载中...",
                commands: {
                    update: "更新",
                    canceledit: "取消"
                }
            },
            filterable: {
                extra: false,
                messages: {
                    isFalse: "停用",
                    isTrue: "启用"
                },
                operators: {
                    string: {
                        contains: "包含",
                        doesnotcontain: "不包含",
                        eq: "等于",
                        neq: "不等于"
                    },
                    number: {
                        contains: "包含",
                        doesnotcontain: "不包含",
                        eq: "等于",
                        neq: "不等于"
                    }
                }
            },
            sortable: true,
            remove: treeList_remove,
            columns: [
                {
                    field: "name",
                    title: "类别名称"
                },
                {
                    field: "status",
                    title: "状态",
                    editor: StatusSwitchEditor,
                    template: function (e) {
                        if (e.status)
                            return '启用';
                        return '停用';
                    },
                    width: 100
                },
                {
                    field: "sort",
                    title: "排序",
                    attributes: alignCenterAttribute,
                    width: 100
                },
                {
                    title: "",
                    command: [{name: "create", text: "新增"}, {name: "edit", text: "修改"}, {name: "destroy", text: "删除"}],
                    width: 300
                }
            ]
        });
    });

    function StatusSwitchEditor(container, options) {
        var checked = options.model.status;
        $('<input id="department-type-edit-switch" type="checkbox" name="' + options.field + '" data-role="switch" class="km-widget"/>')
            .appendTo(container)
            .kendoMobileSwitch({
                checked: checked,
                onLabel: "启用",
                offLabel: "停用",
                change: function (e) {
                    if (!e.checked) {
                        if (options.model.hasChildren){
                            layer.confirm('', {
                                icon: 0,
                                btn: ['確認', '取消'],
                                closeBtn:0,
                                title:"提示信息",
                                content:"是否確認停用此類別，停用后此類別下的所有類別也會相應停用，請確認清楚再操作！<br/>如已確認，請點擊“確認”按鈕，如需確認清楚，請點擊“取消”！"
                            }, function (index) {
                                layer.close(index);
                            }, function () {
                                $("#department-type-edit-switch").data("kendoMobileSwitch").toggle();
                            });
                        }
                    }
                }
            });
    }

    function treeList_remove(e) {
        e.preventDefault();
        layer.msg('你确定要删除“' + e.model.name + '”？', {
            time: 0 //不自动关闭
            , btn: ['确定', '取消']
            , shade: [0.5]
            , yes: function (index) {
                layer.close(index);
                $.ajax({
                    type: "POST",
                    url: "<?=Url::toRoute(['/hr/pick/default/destroyepartmenttypeedit']); ?>",
                    data: {id: e.model.id},
                    dataType: "json",
                    success: function (result) {
                        if (result.status == 1) {
                            //$(e.row).remove();
                            //$("[data-uid='"+e.model.uid+"']").remove();
                            var h_grid = $("#Home-Type-Edit-setting-List").data("kendoTreeList");
                            h_grid.dataSource.read();
                            h_grid.refresh();
                        } else {
                            layer.msg(result.msg, {icon: 5});
                        }
                    }
                });
            }
        });
    }
</script>
